﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Solution.Business.Component;
using Solution.Business.Entities;
using Solution.Data;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using Solution.UI.Web.Models;
using System.Data;
using MvcPaging;

namespace Solution.UI.Web.Controllers.Admin
{
    
    public class AdminNewsController : Controller
    {
        //
        // GET: /AdminNews/
        private Solution.Business.Entities.SolutionEntities db = new SolutionEntities();
        private const int DefaultPageSize = 20;
        private IList<News> listnews;

        public ActionResult Index(int? page)
        {
            var list = db.News.OrderByDescending(c => c.NewsId);
            listnews = list.ToList<News>();
            var list1 = Business.Component.LocationComponent.Location_GetByTop("1000", "len([Level]) = 4", "");
            ViewBag.TinhId = new SelectList(list1, "LId", "Name");

            var list2 = Business.Component.TransactionComponent.Transaction_GetByTop("1000", "len([Level]) = 4", "");
            ViewBag.TrId = new SelectList(list2, "TrId", "Name");

            int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            return View(this.listnews.ToPagedList(currentPageIndex, DefaultPageSize));
        }

        [HttpPost]
        public ActionResult Index(News news, int? page, FormCollection form, string submitButton)
        {

            IList<News> list;
            if (submitButton == "Hiển thị")
            {
                List<int> listLocation = new List<int>();
                //int location = Int32.Parse(form.GetValues("TinhId").First());
                if (form.GetValues("TinhId").First() != "")
                {
                    int location = Int32.Parse(form.GetValues("TinhId").First());
                    var locationId = Business.Component.LocationComponent.Location_GetByTop("1", "LGroup = (select LGroup from Location where LId = '"+ location +"')", "");
                    int LGroup = Int32.Parse(locationId[0].LGroup.ToString());
                    listLocation.Add(LGroup);
                }
                else
                {
                    listLocation = (from item in db.Locations
                                    select item.LId).ToList<int>();

                }

                List<int> listTransaction = new List<int>();
                //int transaction = Int32.Parse(form.GetValues("TrId").First());
                if (form.GetValues("TrId").First() != "")
                {
                    listTransaction.Add(Int32.Parse(form.GetValues("TrId").First()));
                }
                else
                {
                    listTransaction = (from item in db.Transactions
                                       select item.TrId).ToList<int>();

                }

                List<int> listPosition = new List<int>();
                int position = Int32.Parse(form.GetValues("Position").First());
                if (position != 0)
                {
                    listPosition.Add(Int32.Parse(form.GetValues("Position").First()));
                }
                else
                {
                    listPosition.Add(1);
                    listPosition.Add(2);
                }

                string txtTitle = form.GetValues("txtSearch") != null ? form.GetValues("txtSearch").First() : "";

                var query = from item in db.News
                            where listLocation.Contains(item.Location.LGroup.Value) &&
                                  listTransaction.Contains(item.TrId.Value) &&
                                  listPosition.Contains(item.Position.Value) &&
                                  item.Title.Contains(txtTitle)
                            select item;
                listnews = query.ToList<News>();
                var list1 = Business.Component.LocationComponent.Location_GetByTop("1000", "len([Level]) = 4", "");
                ViewBag.TinhId = new SelectList(list1, "LId", "Name");

                var list2 = Business.Component.TransactionComponent.Transaction_GetByTop("1000", "len([Level]) = 4", "");
                ViewBag.TrId = new SelectList(list2, "TrId", "Name");
                int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
                return View(this.listnews.ToPagedList(currentPageIndex, DefaultPageSize));
            }  
            return RedirectToAction("Index");
        }

        public ActionResult Details(int id = 0)
        {
            News news = db.News.Find(id);
            if (news == null)
            {
                return HttpNotFound();
            }
            return View(news);
        }

        public ActionResult Create()
        {
            var list = Business.Component.PaymentComponent.Payments_GetByTop("50", "", "");
            ViewBag.PmId = new SelectList(list, "PmId", "Name");

            var list1 = Business.Component.UnitComponent.Unit_GetByTop("50", "", "");
            ViewBag.UnId = new SelectList(list1, "UnId", "Name");

            var list2 = Business.Component.TransactionComponent.Transaction_GetByTop("50", "len([Level]) > 4", "");
            ViewBag.TrId = new SelectList(list2, "TrId", "Name");

            var list3 = Business.Component.LocationComponent.Location_GetByTop("50", "len([Level]) = 4", "");
            ViewBag.TinhId = new SelectList(list3, "LId", "Name");

            var list4 = Business.Component.DirectionComponent.Direction_GetByTop("50", "", "");
            ViewBag.DId = new SelectList(list4, "DId", "Name");

            var list5 = Business.Component.LegalComponent.Legal_GetByTop("50", "", "");
            ViewBag.LgId = new SelectList(list5, "LgId", "Name");
            return View();
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(News model, int Position, int Status , HttpPostedFileBase file, HttpPostedFileBase file1, HttpPostedFileBase file2, HttpPostedFileBase file3, HttpPostedFileBase file4, HttpPostedFileBase file5, HttpPostedFileBase file6, HttpPostedFileBase file7, HttpPostedFileBase file8, HttpPostedFileBase file9, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                if(Position == 0)
                {
                    model.Position = 1;
                }
                else if (Position == 1)
                {
                    model.Position = 2;
                }

                if (Status == 1)
                {
                    model.Status = 1;
                }
                else if (Status == 2)
                {
                    model.Status = 2;
                }

                model.Date = DateTime.Now;
                model.LId = Int32.Parse(form.GetValues("QuanId").First());
                Thongke thongke = db.Thongkes.Where(c => c.TrId == model.TrId && c.Month == DateTime.Now.Month).First();
                thongke.Count++;
                if (model.Title != null && model.Content != null && model.Price != null && model.PmId != null && model.Acreage != null && model.UnId != null && model.TrId != null && model.LId != null && model.Position != null && model.Status != null && file != null)
                {
                    db.News.Add(model);
                    db.Entry(thongke).State = EntityState.Modified;
                    db.SaveChanges();
                //}
                //else
                //{
                //    ModelState.AddModelError("", "Bạn chưa điền đầy đủ thông tin cần nhập !");
                //}
                if (file != null && file.ContentLength > 0)
                {


                    var temp = (from element in db.News
                                where element.NewsId.Equals(model.NewsId)
                                select element).FirstOrDefault();

                    if (temp != null)
                    {
                        temp.Images = "/images/" + temp.NewsId + "_" + file.FileName;
                        //var filePath = Path.Combine(Server.MapPath("~/upload/"), temp.CauHoiID + file.FileName);
                        var filePath = Path.Combine(Server.MapPath("~/images/"), temp.NewsId + "_" + file.FileName);
                        var images = Image.FromStream(file.InputStream);
                        var newImage = ImageScale.ScaleImage(images, 372, 200);
                        newImage.Save(filePath);
                    }

                    db.SaveChanges();
                }

                if (file1 != null && file1.ContentLength > 0)
                {


                    var temp = (from element in db.News
                                where element.NewsId.Equals(model.NewsId)
                                select element).FirstOrDefault();

                    if (temp != null)
                    {
                        temp.Image1 = "/images/" + temp.NewsId + "_" + file1.FileName;
                        //var filePath = Path.Combine(Server.MapPath("~/upload/"), temp.CauHoiID + file.FileName);
                        var filePath1 = Path.Combine(Server.MapPath("~/images/"), temp.NewsId + "_" + file1.FileName);
                        var image1 = Image.FromStream(file1.InputStream);
                        var newImage1 = ImageScale.ScaleImage(image1, 372, 200);
                        newImage1.Save(filePath1);
                    }

                    db.SaveChanges();
                }

                if (file2 != null && file2.ContentLength > 0)
                {


                    var temp = (from element in db.News
                                where element.NewsId.Equals(model.NewsId)
                                select element).FirstOrDefault();

                    if (temp != null)
                    {
                        temp.Image2 = "/images/" + temp.NewsId + "_" + file2.FileName;
                        //var filePath = Path.Combine(Server.MapPath("~/upload/"), temp.CauHoiID + file.FileName);
                        var filePath2 = Path.Combine(Server.MapPath("~/images/"), temp.NewsId + "_" + file2.FileName);
                        var image2 = Image.FromStream(file2.InputStream);
                        var newImage2 = ImageScale.ScaleImage(image2, 372, 200);
                        newImage2.Save(filePath2);
                    }

                    db.SaveChanges();
                }

                if (file3 != null && file3.ContentLength > 0)
                {


                    var temp = (from element in db.News
                                where element.NewsId.Equals(model.NewsId)
                                select element).FirstOrDefault();

                    if (temp != null)
                    {
                        temp.Image3 = "/images/" + temp.NewsId + "_" + file3.FileName;
                        //var filePath = Path.Combine(Server.MapPath("~/upload/"), temp.CauHoiID + file.FileName);
                        var filePath3 = Path.Combine(Server.MapPath("~/images/"), temp.NewsId + "_" + file3.FileName);
                        var image3 = Image.FromStream(file3.InputStream);
                        var newImage3 = ImageScale.ScaleImage(image3, 372, 200);
                        newImage3.Save(filePath3);
                    }

                    db.SaveChanges();
                }

                if (file4 != null && file4.ContentLength > 0)
                {


                    var temp = (from element in db.News
                                where element.NewsId.Equals(model.NewsId)
                                select element).FirstOrDefault();

                    if (temp != null)
                    {
                        temp.Image4 = "/images/" + temp.NewsId + "_" + file4.FileName;
                        //var filePath = Path.Combine(Server.MapPath("~/upload/"), temp.CauHoiID + file.FileName);
                        var filePath4 = Path.Combine(Server.MapPath("~/images/"), temp.NewsId + "_" + file4.FileName);
                        var image4 = Image.FromStream(file4.InputStream);
                        var newImage4 = ImageScale.ScaleImage(image4, 372, 200);
                        newImage4.Save(filePath4);
                    }

                    db.SaveChanges();
                }

                if (file5 != null && file5.ContentLength > 0)
                {


                    var temp = (from element in db.News
                                where element.NewsId.Equals(model.NewsId)
                                select element).FirstOrDefault();

                    if (temp != null)
                    {
                        temp.Image5 = "/images/" + temp.NewsId + "_" + file5.FileName;
                        //var filePath = Path.Combine(Server.MapPath("~/upload/"), temp.CauHoiID + file.FileName);
                        var filePath5 = Path.Combine(Server.MapPath("~/images/"), temp.NewsId + "_" + file5.FileName);
                        var image5 = Image.FromStream(file5.InputStream);
                        var newImage5 = ImageScale.ScaleImage(image5, 372, 200);
                        newImage5.Save(filePath5);
                    }

                    db.SaveChanges();
                }

                if (file6 != null && file6.ContentLength > 0)
                {


                    var temp = (from element in db.News
                                where element.NewsId.Equals(model.NewsId)
                                select element).FirstOrDefault();

                    if (temp != null)
                    {
                        temp.Image6 = "/images/" + temp.NewsId + "_" + file6.FileName;
                        //var filePath = Path.Combine(Server.MapPath("~/upload/"), temp.CauHoiID + file.FileName);
                        var filePath6 = Path.Combine(Server.MapPath("~/images/"), temp.NewsId + "_" + file6.FileName);
                        var image6 = Image.FromStream(file6.InputStream);
                        var newImage6 = ImageScale.ScaleImage(image6, 372, 200);
                        newImage6.Save(filePath6);
                    }

                    db.SaveChanges();
                }

                if (file7 != null && file7.ContentLength > 0)
                {


                    var temp = (from element in db.News
                                where element.NewsId.Equals(model.NewsId)
                                select element).FirstOrDefault();

                    if (temp != null)
                    {
                        temp.Image7 = "/images/" + temp.NewsId + "_" + file7.FileName;
                        //var filePath = Path.Combine(Server.MapPath("~/upload/"), temp.CauHoiID + file.FileName);
                        var filePath7 = Path.Combine(Server.MapPath("~/images/"), temp.NewsId + "_" + file7.FileName);
                        var image7 = Image.FromStream(file7.InputStream);
                        var newImage7 = ImageScale.ScaleImage(image7, 372, 200);
                        newImage7.Save(filePath7);
                    }

                    db.SaveChanges();
                }

                if (file8 != null && file8.ContentLength > 0)
                {


                    var temp = (from element in db.News
                                where element.NewsId.Equals(model.NewsId)
                                select element).FirstOrDefault();

                    if (temp != null)
                    {
                        temp.Image8 = "/images/" + temp.NewsId + "_" + file8.FileName;
                        //var filePath = Path.Combine(Server.MapPath("~/upload/"), temp.CauHoiID + file.FileName);
                        var filePath8 = Path.Combine(Server.MapPath("~/images/"), temp.NewsId + "_" + file8.FileName);
                        var image8 = Image.FromStream(file8.InputStream);
                        var newImage8 = ImageScale.ScaleImage(image8, 372, 200);
                        newImage8.Save(filePath8);
                    }

                    db.SaveChanges();
                }

                if (file9 != null && file9.ContentLength > 0)
                {


                    var temp = (from element in db.News
                                where element.NewsId.Equals(model.NewsId)
                                select element).FirstOrDefault();

                    if (temp != null)
                    {
                        temp.Image9 = "/images/" + temp.NewsId + "_" + file9.FileName;
                        //var filePath = Path.Combine(Server.MapPath("~/upload/"), temp.CauHoiID + file.FileName);
                        var filePath9 = Path.Combine(Server.MapPath("~/images/"), temp.NewsId + "_" + file9.FileName);
                        var image9 = Image.FromStream(file9.InputStream);
                        var newImage9 = ImageScale.ScaleImage(image9, 372, 200);
                        newImage9.Save(filePath9);
                    }

                    db.SaveChanges();
                }
                    return RedirectToAction("Index", "AdminNews");
                }
                else
                {
                    ModelState.AddModelError("", "Bạn chưa điền đầy đủ thông tin cần nhập vào ô có dấu * !");
                }

                //return RedirectToAction("Index", "AdminNews");
            }
            var list = Business.Component.PaymentComponent.Payments_GetByTop("50", "", "");
            ViewBag.PmId = new SelectList(list, "PmId", "Name");

            var list1 = Business.Component.UnitComponent.Unit_GetByTop("50", "", "");
            ViewBag.UnId = new SelectList(list1, "UnId", "Name");

            var list2 = Business.Component.TransactionComponent.Transaction_GetByTop("50", "len([Level]) > 4", "");
            ViewBag.TrId = new SelectList(list2, "TrId", "Name");

            var list3 = Business.Component.LocationComponent.Location_GetByTop("50", "len([Level]) = 4", "");
            ViewBag.TinhId = new SelectList(list3, "LId", "Name");

            var list4 = Business.Component.DirectionComponent.Direction_GetByTop("50", "", "");
            ViewBag.DId = new SelectList(list4, "DId", "Name");

            var list5 = Business.Component.LegalComponent.Legal_GetByTop("50", "", "");
            ViewBag.LgId = new SelectList(list5, "LgId", "Name");
            return View(model);
        }

        [HttpPost]
        public ActionResult GetQuanId1(int TinhId)
        {
            var list = Business.Component.LocationComponent.Location_GetByTop("50", "len([Level]) > 4 and left([Level], 4) = (select [Level] from Location where LId = '" + TinhId + "')", "");
            return PartialView(list);
        }

        public ActionResult Edit(int id = 0)
        {
            var news = db.News.Find(id);
            if (news == null)
            {
                return HttpNotFound();
            }
            var list = Business.Component.PaymentComponent.Payments_GetByTop("50", "", "");
            ViewBag.PmId = new SelectList(list, "PmId", "Name", news.Payment.Name);

            var list1 = Business.Component.UnitComponent.Unit_GetByTop("50", "", "");
            ViewBag.UnId = new SelectList(list1, "UnId", "Name", news.Unit.Name);

            var list2 = Business.Component.TransactionComponent.Transaction_GetByTop("50", "len([Level]) > 4", "");
            ViewBag.TrId = new SelectList(list2, "TrId", "Name", news.Transaction.Name);

            var list3 = Business.Component.LocationComponent.Location_GetByTop("50", "len([Level]) > 4", "");
            ViewBag.LId = new SelectList(list3, "LId", "Name", news.Location.Name);

            var list4 = Business.Component.DirectionComponent.Direction_GetByTop("50", "", "");
            if (news.DId != null)
            {
                ViewBag.DId = new SelectList(list4, "DId", "Name", news.Direction.Name);
            }
            else
            {
                ViewBag.DId = new SelectList(list4, "DId", "Name", "");
            }

            var list5 = Business.Component.LegalComponent.Legal_GetByTop("50", "", "");
            if (news.LgId != null)
            {
                ViewBag.LgId = new SelectList(list5, "LgId", "Name", news.Legal.Name);
            }
            else
            {
                ViewBag.LgId = new SelectList(list5, "LgId", "Name", "");
            }
            
            return View(news);
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(News news, int Position, int Status , HttpPostedFileBase file, HttpPostedFileBase file1, HttpPostedFileBase file2, HttpPostedFileBase file3, HttpPostedFileBase file4, HttpPostedFileBase file5, HttpPostedFileBase file6, HttpPostedFileBase file7, HttpPostedFileBase file8, HttpPostedFileBase file9)
        {
            if (ModelState.IsValid)
            {
                if (file != null && file.ContentLength > 0)
                {
                    if (news.Images != null)
                    {
                        var oldFilePath = Server.MapPath("~") + news.Images.Replace("/", "\\").TrimStart('\\');
                        if (System.IO.File.Exists(oldFilePath))
                        {
                            System.IO.File.Delete(oldFilePath);
                        }
                    }
                    news.Images = "/images/" + news.NewsId + "_" + file.FileName;
                    //var filePath = Path.Combine(Server.MapPath("~/upload/"), file.FileName);
                    var filePath = Path.Combine(Server.MapPath("~/images/"), news.NewsId + "_" + file.FileName);
                    var image = Image.FromStream(file.InputStream);
                    var newImage = ImageScale.ScaleImage(image, 372, 200);
                    newImage.Save(filePath);

                     //db.SaveChanges();
                }

                if (file1 != null && file1.ContentLength > 0)
                {
                    if (news.Image1 != null)
                    {
                        var oldFilePath1 = Server.MapPath("~") + news.Image1.Replace("/", "\\").TrimStart('\\');
                        if (System.IO.File.Exists(oldFilePath1))
                        {
                            System.IO.File.Delete(oldFilePath1);
                        }
                    }
                    news.Image1 = "/images/" + news.NewsId + "_" + file1.FileName;
                    //var filePath = Path.Combine(Server.MapPath("~/upload/"), file.FileName);
                    var filePath1 = Path.Combine(Server.MapPath("~/images/"), news.NewsId + "_" + file1.FileName);
                    var image1 = Image.FromStream(file1.InputStream);
                    var newImage1 = ImageScale.ScaleImage(image1, 372, 200);
                    newImage1.Save(filePath1);

                     //db.SaveChanges();
                }

                if (file2 != null && file2.ContentLength > 0)
                {
                    if (news.Image2 != null)
                    {
                        var oldFilePath2 = Server.MapPath("~") + news.Image2.Replace("/", "\\").TrimStart('\\');
                        if (System.IO.File.Exists(oldFilePath2))
                        {
                            System.IO.File.Delete(oldFilePath2);
                        }
                    }
                    news.Image2 = "/images/" + news.NewsId + "_" + file2.FileName;
                    //var filePath = Path.Combine(Server.MapPath("~/upload/"), file.FileName);
                    var filePath2 = Path.Combine(Server.MapPath("~/images/"), news.NewsId + "_" + file2.FileName);
                    var image2 = Image.FromStream(file2.InputStream);
                    var newImage2 = ImageScale.ScaleImage(image2, 372, 200);
                    newImage2.Save(filePath2);

                     //db.SaveChanges();
                }

                if (file3 != null && file3.ContentLength > 0)
                {
                    if (news.Image3 != null)
                    {
                        var oldFilePath3 = Server.MapPath("~") + news.Image3.Replace("/", "\\").TrimStart('\\');
                        if (System.IO.File.Exists(oldFilePath3))
                        {
                            System.IO.File.Delete(oldFilePath3);
                        }
                    }
                    news.Image3 = "/images/" + news.NewsId + "_" + file3.FileName;
                    //var filePath = Path.Combine(Server.MapPath("~/upload/"), file.FileName);
                    var filePath3 = Path.Combine(Server.MapPath("~/images/"), news.NewsId + "_" + file3.FileName);
                    var image3 = Image.FromStream(file3.InputStream);
                    var newImage3 = ImageScale.ScaleImage(image3, 372, 200);
                    newImage3.Save(filePath3);

                     //db.SaveChanges();
                }

                if (file4 != null && file4.ContentLength > 0)
                {
                    if (news.Image4 != null)
                    {
                        var oldFilePath4 = Server.MapPath("~") + news.Image4.Replace("/", "\\").TrimStart('\\');
                        if (System.IO.File.Exists(oldFilePath4))
                        {
                            System.IO.File.Delete(oldFilePath4);
                        }
                    }
                    news.Image4 = "/images/" + news.NewsId + "_" + file4.FileName;
                    //var filePath = Path.Combine(Server.MapPath("~/upload/"), file.FileName);
                    var filePath4 = Path.Combine(Server.MapPath("~/images/"), news.NewsId + "_" + file4.FileName);
                    var image4 = Image.FromStream(file4.InputStream);
                    var newImage4 = ImageScale.ScaleImage(image4, 372, 200);
                    newImage4.Save(filePath4);

                     //db.SaveChanges();
                }

                if (file5 != null && file5.ContentLength > 0)
                {
                    if (news.Image5 != null)
                    {
                        var oldFilePath5 = Server.MapPath("~") + news.Image5.Replace("/", "\\").TrimStart('\\');
                        if (System.IO.File.Exists(oldFilePath5))
                        {
                            System.IO.File.Delete(oldFilePath5);
                        }
                    }
                    news.Image5 = "/images/" + news.NewsId + "_" + file5.FileName;
                    //var filePath = Path.Combine(Server.MapPath("~/upload/"), file.FileName);
                    var filePath5 = Path.Combine(Server.MapPath("~/images/"), news.NewsId + "_" + file5.FileName);
                    var image5 = Image.FromStream(file5.InputStream);
                    var newImage5 = ImageScale.ScaleImage(image5, 372, 200);
                    newImage5.Save(filePath5);

                    //db.SaveChanges();
                }

                if (file6 != null && file6.ContentLength > 0)
                {
                    if (news.Image6 != null)
                    {
                        var oldFilePath6 = Server.MapPath("~") + news.Image6.Replace("/", "\\").TrimStart('\\');
                        if (System.IO.File.Exists(oldFilePath6))
                        {
                            System.IO.File.Delete(oldFilePath6);
                        }
                    }
                    news.Image6 = "/images/" + news.NewsId + "_" + file6.FileName;
                    //var filePath = Path.Combine(Server.MapPath("~/upload/"), file.FileName);
                    var filePath6 = Path.Combine(Server.MapPath("~/images/"), news.NewsId + "_" + file6.FileName);
                    var image6 = Image.FromStream(file6.InputStream);
                    var newImage6 = ImageScale.ScaleImage(image6, 372, 200);
                    newImage6.Save(filePath6);

                     //db.SaveChanges();
                }

                if (file7 != null && file7.ContentLength > 0)
                {
                    if (news.Image7 != null)
                    {
                        var oldFilePath7 = Server.MapPath("~") + news.Image7.Replace("/", "\\").TrimStart('\\');
                        if (System.IO.File.Exists(oldFilePath7))
                        {
                            System.IO.File.Delete(oldFilePath7);
                        }
                    }
                    news.Image7 = "/images/" + news.NewsId + "_" + file7.FileName;
                    //var filePath = Path.Combine(Server.MapPath("~/upload/"), file.FileName);
                    var filePath7 = Path.Combine(Server.MapPath("~/images/"), news.NewsId + "_" + file7.FileName);
                    var image7 = Image.FromStream(file7.InputStream);
                    var newImage7 = ImageScale.ScaleImage(image7, 372, 200);
                    newImage7.Save(filePath7);

                     //db.SaveChanges();
                }

                if (file8 != null && file8.ContentLength > 0)
                {
                    if (news.Image8 != null)
                    {
                        var oldFilePath8 = Server.MapPath("~") + news.Image8.Replace("/", "\\").TrimStart('\\');
                        if (System.IO.File.Exists(oldFilePath8))
                        {
                            System.IO.File.Delete(oldFilePath8);
                        }
                    }
                    news.Image8 = "/images/" + news.NewsId + "_" + file8.FileName;
                    //var filePath = Path.Combine(Server.MapPath("~/upload/"), file.FileName);
                    var filePath8 = Path.Combine(Server.MapPath("~/images/"), news.NewsId + "_" + file8.FileName);
                    var image8 = Image.FromStream(file8.InputStream);
                    var newImage8 = ImageScale.ScaleImage(image8, 372, 200);
                    newImage8.Save(filePath8);

                     //db.SaveChanges();
                }

                if (file9 != null && file9.ContentLength > 0)
                {
                    if (news.Image9 != null)
                    {
                        var oldFilePath9 = Server.MapPath("~") + news.Image9.Replace("/", "\\").TrimStart('\\');
                        if (System.IO.File.Exists(oldFilePath9))
                        {
                            System.IO.File.Delete(oldFilePath9);
                        }
                    }
                    news.Image9 = "/images/" + news.NewsId + "_" + file9.FileName;
                    //var filePath = Path.Combine(Server.MapPath("~/upload/"), file.FileName);
                    var filePath9 = Path.Combine(Server.MapPath("~/images/"), news.NewsId + "_" + file9.FileName);
                    var image9 = Image.FromStream(file9.InputStream);
                    var newImage9 = ImageScale.ScaleImage(image9, 372, 200);
                    newImage9.Save(filePath9);

                     //db.SaveChanges();
                }

                if (Position == 0)
                {
                    news.Position = 1;
                }
                else if (Position == 1)
                {
                    news.Position = 2;
                }

                if (Status == 1)
                {
                    news.Status = 1;
                }
                else if (Status == 2)
                {
                    news.Status = 2;
                }
                db.Entry(news).State = EntityState.Modified;
                if (news.Title != null && news.Content != null && news.Price != null && news.PmId != null && news.Acreage != null && news.UnId != null && news.TrId != null && news.LId != null && news.Position != null && news.Status != null && file != null)
                {
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError("", "Bạn chưa điền đầy đủ thông tin cần nhập vào ô có dấu * !");
                }
            }

            var list = Business.Component.PaymentComponent.Payments_GetByTop("50", "", "");
            ViewBag.PmId = new SelectList(list, "PmId", "Name", news.Payment.Name);

            var list1 = Business.Component.UnitComponent.Unit_GetByTop("50", "", "");
            ViewBag.UnId = new SelectList(list1, "UnId", "Name", news.Unit.Name);

            var list2 = Business.Component.TransactionComponent.Transaction_GetByTop("50", "len([Level]) > 4", "");
            ViewBag.TrId = new SelectList(list2, "TrId", "Name", news.Transaction.Name);

            var list3 = Business.Component.LocationComponent.Location_GetByTop("50", "len([Level]) > 4", "");
            ViewBag.LId = new SelectList(list3, "LId", "Name", news.Location.Name);

            var list4 = Business.Component.DirectionComponent.Direction_GetByTop("50", "", "");
            if (news.DId != null)
            {
                ViewBag.DId = new SelectList(list4, "DId", "Name", news.Direction.Name);
            }
            else
            {
                ViewBag.DId = new SelectList(list4, "DId", "Name", "");
            }

            var list5 = Business.Component.LegalComponent.Legal_GetByTop("50", "", "");
            if (news.LgId != null)
            {
                ViewBag.LgId = new SelectList(list5, "LgId", "Name", news.Legal.Name);
            }
            else
            {
                ViewBag.LgId = new SelectList(list5, "LgId", "Name", "");
            }

            return View(news);
        }

        public ActionResult Delete(int id = 0)
        {
            News news = db.News.Find(id);
            if (news == null)
            {
                return HttpNotFound();
            }
            return View(news);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            var news = db.News.Find(id);

            if (news.Images != null)
            {
                var filePath = Server.MapPath("~") + news.Images.Replace("/", "\\").TrimStart('\\');
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }
            }

            if (news.Image1 != null)
            {
                var filePath1 = Server.MapPath("~") + news.Image1.Replace("/", "\\").TrimStart('\\');
                if (System.IO.File.Exists(filePath1))
                {
                    System.IO.File.Delete(filePath1);
                }
            }

            if (news.Image2 != null)
            {
                var filePath2 = Server.MapPath("~") + news.Image2.Replace("/", "\\").TrimStart('\\');
                if (System.IO.File.Exists(filePath2))
                {
                    System.IO.File.Delete(filePath2);
                }
            }

            if (news.Image3 != null)
            {
                var filePath3 = Server.MapPath("~") + news.Image3.Replace("/", "\\").TrimStart('\\');
                if (System.IO.File.Exists(filePath3))
                {
                    System.IO.File.Delete(filePath3);
                }
            }

            if (news.Image4 != null)
            {
                var filePath4 = Server.MapPath("~") + news.Image4.Replace("/", "\\").TrimStart('\\');
                if (System.IO.File.Exists(filePath4))
                {
                    System.IO.File.Delete(filePath4);
                }
            }

            if (news.Image5 != null)
            {
                var filePath5 = Server.MapPath("~") + news.Image5.Replace("/", "\\").TrimStart('\\');
                if (System.IO.File.Exists(filePath5))
                {
                    System.IO.File.Delete(filePath5);
                }
            }

            if (news.Image6 != null)
            {
                var filePath6 = Server.MapPath("~") + news.Image6.Replace("/", "\\").TrimStart('\\');
                if (System.IO.File.Exists(filePath6))
                {
                    System.IO.File.Delete(filePath6);
                }
            }

            if (news.Image7 != null)
            {
                var filePath7 = Server.MapPath("~") + news.Image7.Replace("/", "\\").TrimStart('\\');
                if (System.IO.File.Exists(filePath7))
                {
                    System.IO.File.Delete(filePath7);
                }
            }

            if (news.Image8 != null)
            {
                var filePath8 = Server.MapPath("~") + news.Image8.Replace("/", "\\").TrimStart('\\');
                if (System.IO.File.Exists(filePath8))
                {
                    System.IO.File.Delete(filePath8);
                }
            }

            if (news.Image9 != null)
            {
                var filePath9 = Server.MapPath("~") + news.Image9.Replace("/", "\\").TrimStart('\\');
                if (System.IO.File.Exists(filePath9))
                {
                    System.IO.File.Delete(filePath9);
                }
            }

            db.News.Remove(news);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Hide(int id = 0)
        {
            News news = db.News.Find(id);
            if (news == null)
            {
                return HttpNotFound();
            }
            return View(news);
        }

        [HttpPost, ActionName("Hide")]
        public ActionResult HideNews(int id)
        {
            var news = db.News.Find(id);
            news.Status = 2;
            db.Entry(news).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

    }
}
